﻿using System;

namespace c1
{
    public class Node<T>
    {
        public Node<T> next
        {
            get;
            set;
        }
        public T data
        {
            get;
            set;
        }
        public Node(T t)
        {
            this.data = t;
            this.next = null;
        }

    }
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            head = tail = null;
        }
        public Node<T> Head
        {
            get=>head;
        }
        public void add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                tail = head = n;
            }
            else
            {
                tail.next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            
            Node<T> temp = head;
            while (temp.next != null)
            {
                action(temp.data);
                temp = temp.next;
            }
            action(temp.data);

        }


    }
    delegate int Fun(int i, int j);
    class Program
    {
        
        static void Main(string[] args)
        {                      
            int max = -9999;
            int min = 9999;
            int sum = 0;
            //lambda表达式：
            Action<int> printNodes = delegate (int item){Console.WriteLine(item);};
            Action<int> findMax = delegate (int i) { if (max < i) max = i; };
            Action<int> findMin = delegate (int i) { if (i < min) min = i; };
            Action<int> getSum = delegate (int i) { sum += i; };
            GenericList<int> intList = new GenericList<int>();
            intList.add(21);
            intList.add(42);
            intList.add(78);
            intList.add(16);
            intList.add(58);
            Console.WriteLine("打印元素：");
            intList.ForEach(printNodes);
            //求最大值
            Console.WriteLine("求最大值:");
            //int max = -9999;
            intList.ForEach(findMax);
            Console.WriteLine(max);
            //求最小值
            Console.WriteLine("求最小值:");
            //int min = 9999;
            intList.ForEach(findMin);
            Console.WriteLine(min);
            //求和
            Console.WriteLine("求和:");
            //int sum = 0;
            intList.ForEach(getSum);
            Console.WriteLine(sum);

        }
    }
}
